package cn.hd.service.impl;

import cn.hd.dao.ProductdetailDao;
import cn.hd.entity.ProductLevel;
import cn.hd.entity.Productdetail;
import cn.hd.service.ProductdetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
@Transactional

public class ProductdetailServiceImpl implements ProductdetailService {
    @Autowired
    private ProductdetailDao dao;
    @Override
    public PageInfo<Productdetail> selectProductdetail(int pageNum,int pageSize,Map map) {
        PageHelper.startPage(pageNum,pageSize);
        List<Productdetail> productdetails = dao.selectProductdetail(map);
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sf.format(date);
        for (Productdetail p : productdetails) {
            if (p.getOverdate().equals(date)){
                //调用修改状态值方法
                dao.updateStatusFailed(p.getPid());
            }
            if(p.getPricenum()==p.getPricenow()){
                //调用成功方法修改状态值
                dao.updateStatusSuccess(p.getPid());
            }
            String s = dao.selectUserNamePro(p.getPid());
            p.setFqr(s);
            double a = p.getPricenow();
          double b = p.getPricenum();
          double c = (a/b) * 100;
          int bfb = (int) c;
          p.setBfb(bfb);
        }
        PageInfo<Productdetail> page = new PageInfo<>(productdetails);

        return page;
    }

    //成功
    @Override
    public PageInfo<Productdetail> selectProductdetailSuccess(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Productdetail> productdetails = dao.selectProductdetailSuccess();
        for (Productdetail p : productdetails) {
            double a = p.getPricenow();
            double b = p.getPricenum();
            double c = (a/b) * 100;
            int bfb = (int) c;
            p.setBfb(bfb);
            String s = dao.selectUserNamePro(p.getPid());
            p.setFqr(s);
        }
        PageInfo<Productdetail> page = new PageInfo<>(productdetails);
        return page;
    }
    //进行中
    @Override
    public PageInfo<Productdetail> selectProductdetailIng(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Productdetail> productdetails = dao.selectProductdetailIng();
        for (Productdetail p : productdetails) {
            double a = p.getPricenow();
            double b = p.getPricenum();
            double c = (a/b) * 100;
            int bfb = (int) c;
            p.setBfb(bfb);
            String s = dao.selectUserNamePro(p.getPid());
            p.setFqr(s);
        }
        PageInfo<Productdetail> page = new PageInfo<>(productdetails);
        return page;
    }
    //失败
    @Override
    public PageInfo<Productdetail> selectProductdetailFailed(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Productdetail> productdetails = dao.selectProductdetailFailed();
        for (Productdetail p : productdetails) {
            double a = p.getPricenow();
            double b = p.getPricenum();
            double c = (a/b) * 100;
            int bfb = (int) c;
            p.setBfb(bfb);
            String s = dao.selectUserNamePro(p.getPid());
            p.setFqr(s);
        }
        PageInfo<Productdetail> page = new PageInfo<>(productdetails);
        return page;
    }
    //众筹金额多
    @Override
    public PageInfo<Productdetail> selectByMoney(int pageNum, int pageSize,int state) {
        PageHelper.startPage(pageNum,pageSize);
        List<Productdetail> productdetails = dao.selectByMoney(state);
        for (Productdetail p : productdetails) {
            double a = p.getPricenow();
            double b = p.getPricenum();
            double c = (a/b) * 100;
            int bfb = (int) c;
            p.setBfb(bfb);
            String s = dao.selectUserNamePro(p.getPid());
            p.setFqr(s);
        }
        PageInfo<Productdetail> page = new PageInfo<>(productdetails);
        return page;
    }
    //众筹支持人数多
    @Override
    public PageInfo<Productdetail> selectByPerson(int pageNum, int pageSize,int state) {
        PageHelper.startPage(pageNum,pageSize);
        List<Productdetail> productdetails = dao.selectByPerson(state);
        for (Productdetail p : productdetails) {
            double a = p.getPricenow();
            double b = p.getPricenum();
            double c = (a/b) * 100;
            int bfb = (int) c;
            p.setBfb(bfb);
            String s = dao.selectUserNamePro(p.getPid());
            p.setFqr(s);
        }
        PageInfo<Productdetail> page = new PageInfo<>(productdetails);
        return page;
    }
    //近期项目
    @Override
    public PageInfo<Productdetail> selectDateNow(int pageNum, int pageSize,int state) {
        PageHelper.startPage(pageNum,pageSize);
        List<Productdetail> productdetails = dao.selectDateNow(state);
        for (Productdetail p : productdetails) {
            double a = p.getPricenow();
            double b = p.getPricenum();
            double c = (a/b) * 100;
            int bfb = (int) c;
            p.setBfb(bfb);
            String s = dao.selectUserNamePro(p.getPid());
            p.setFqr(s);
        }
        PageInfo<Productdetail> page = new PageInfo<>(productdetails);
        return page;
    }

    @Override
    public Productdetail selectByPid(long pid) {
        return dao.selectByPid(pid);
    }

    @Override
    public Productdetail selectProductDetailById(long id) {
        Productdetail p = dao.selectProductDetailById(id);
            double a = p.getPricenow();
            double b = p.getPricenum();
            double c = (a/b) * 100;
            int bfb = (int) c;
            String s = dao.selectUserNamePro(p.getPid());
                p.setFqr(s);
            p.setBfb(bfb);

        return p;
    }

    @Override
    public PageInfo<Productdetail> selectProductdetailByState(int pageNum,int pageSize,Map map) {
        PageHelper.startPage(pageNum,pageSize);
        List<Productdetail> productdetails = dao.selectProductdetailByState(map);
        for (Productdetail p : productdetails) {
            String fl = dao.selectByPidAndId(p.getPid());
            p.setFl(fl);
            String s = dao.selectUserNamePro(p.getPid());
            p.setFqr(s);
        }
        PageInfo<Productdetail> page = new PageInfo<>(productdetails);
        return page;
    }

    @Override
    public int updateStatuSuccess(long pid) {
        return dao.updateStatuSuccess(pid);
    }

    @Override
    public int updateStatuFailed(long pid) {
        return dao.updateStatuFailed(pid);
    }

    //根据一级name查询二级分类
    @Override
    public List<ProductLevel> selectProductOneBytwo(String name) {
        List<ProductLevel> pro = dao.selectProductOneBytwo(name);
        return  pro;
    }

    @Override
    public int addguanzhu(long uid, long pid) {
        return dao.addguanzhu(uid,pid);
    }

    @Override
    public String selectUserNamePro(long pid) {
        String s = dao.selectUserNamePro(pid);
        return s;
    }

    @Override
    public PageInfo<Productdetail> selectByProShenhe(int pageNum, int pageSize, long uid) {
        PageHelper.startPage(pageNum,pageSize);
        List<Productdetail> productdetails = dao.selectByProShenhe(uid);
        for (Productdetail p : productdetails) {
            double a = p.getPricenow();
            double b = p.getPricenum();
            double c = (a/b) * 100;
            int bfb = (int) c;
            p.setBfb(bfb);
            String s = dao.selectUserNamePro(p.getPid());
            p.setFqr(s);
        }
        PageInfo<Productdetail> page = new PageInfo<>(productdetails);
        return page;
    }

    @Override
    public int updateProState(long pid) {
        return dao.updateProState(pid);
    }

    @Override
    public int delProStatus(long pid) {
        return dao.delProStatus(pid);
    }

    @Override
    public int deluserPocduct(long pid) {
        return dao.deluserPocduct(pid);
    }


    //zjc--开始
    @Override
    public PageInfo<Productdetail>selectFQPro(int pagenum, int pagesize,int uid,int state) {
        PageHelper.startPage(pagenum,pagesize);
        List<Productdetail> pros = dao.selectFQPro(uid,state);

        Date data = new Date();
        //修改众筹状态
        for (Productdetail p : pros) {
            if (p.getOverdate().equals(data)) {
                //调用修改状态值方法
                dao.updateStatusFailed(p.getPid());
            }
            if (p.getPricenum() == p.getPricenow()) {
                //调用成功方法修改状态值
                dao.updateStatusSuccess(p.getPid());
            }
        }
        //打印众筹信息
        for (Productdetail p : pros){
            if (p.getState() == 0){
                p.setZcstatename("众筹ing");
            }else if (p.getState() == 1){
                p.setZcstatename("众筹成功");
            }else if(p.getState() == 2){
                p.setZcstatename("众筹失败");
            }
            int i = 0;
            Calendar cld = Calendar.getInstance();
            cld.setTime(data);
            while (cld.getTime().before(p.getOverdate())){
                cld.add(Calendar.DATE,1);
                i++;
            }
            p.setSyday(i);
            //百分比
            double a = p.getPricenow();
            double b = p.getPricenum();
            double c = (a/b) * 100;
            int bfb = (int) c;
            p.setBfb(bfb);
        }
        PageInfo<Productdetail> page = new PageInfo<>(pros);
        return page;
    }

    @Override
    public PageInfo<Productdetail> selectProByUid(int pagenum, int pagesize, int uid) {
        PageHelper.startPage(pagenum,pagesize);
        List<Productdetail> pros = dao.selectProByUid(uid);
        for (Productdetail p : pros){
            if (p.getState() == 0){
                p.setZcstatename("众筹中");
            }else if (p.getState() == 1){
                p.setZcstatename("众筹成功");
            }else if(p.getState() == 2){
                p.setZcstatename("众筹失败");
            }
        }
        PageInfo<Productdetail> page = new PageInfo<>(pros);
        return page;
    }

    @Override
    public int quxiaoguanzhu(int uid, int pid) {
        return dao.quxiaoguanzhu(uid,pid);
    }
    @Override
    public int addProduct(Productdetail productdetail) {
        int n = 0;
        Calendar cld = Calendar.getInstance();
        cld.setTime(productdetail.getStartdate());
        while (cld.getTime().before(productdetail.getOverdate())){
            cld.add(Calendar.DATE,1);
            n++;
        }
        productdetail.setTime(n);
        int i = dao.addProduct(productdetail);
        return i;
    }

    @Override
    public int addFQpro(long uid, long productid) {
        return dao.addFQpro(uid,productid);
    }

    @Override
    public List<Integer> selectFLzbaa() {
        return  dao.selectFLzbaa();
    }
    @Override
    public List<String> selectFLmc() {
        return dao.selectFLmc();
    }

    @Override
    public Productdetail selectByp(long pid) {
        Productdetail productdetail = dao.selectByPid(pid);
       //众筹状态
        if (productdetail.getState() == 0){
          productdetail.setZcstatename("众筹中");
        }else if (productdetail.getState() == 1){
          productdetail.setZcstatename("众筹成功");
        }else if(productdetail.getState() == 2){
          productdetail.setZcstatename("众筹失败");
        }
        //剩余天数
        int n = 0;
        Calendar cld = Calendar.getInstance();
        Date date = new Date();
        cld.setTime(date);
        while (cld.getTime().before(productdetail.getOverdate())){
            cld.add(Calendar.DATE,1);
            n++;
        }

        productdetail.setSyday(n-1);
        //百分比
        double a = productdetail.getPricenow();
        double b = productdetail.getPricenum();
        double c = (a/b) * 100;
        int bfb = (int) c;
        productdetail.setBfb(bfb);
        return productdetail;
    }

    @Override
    public int deleteProxqbyPid(long pid) {
        Productdetail productdetail = dao.selectByPid(pid);
        if (productdetail.getState() == 0){
            return 0;
        }else {
            int i = dao.deleteProxqbyPid(pid);
            return i;
        }
    }

    @Override
    public int deleteProUserbyPid(long uid, long pid) {
        int i = dao.deleteProUserbyPid(uid, pid);
        return i;
    }

    @Override
    public List<ProductLevel> selectYJ() {
        List<ProductLevel> productLevels = dao.selectYJ();
        for (ProductLevel p : productLevels){
            int i = dao.selectye(p.getId());
            p.setCount(i);
        }
        return productLevels;
    }

    @Override
    public Productdetail selectAllByXiamgMuname(String xnname) {
        return dao.selectAllByXiamgMuname(xnname);
    }

    @Override
    public int updatselectPricenumBypnaem(String pname, double price) {
        return dao.updatselectPricenumBypnaem(pname,price);
    }

    @Override
    public int updateZCjia1(String pname) {
        return dao.updateZCjia1(pname);
    }

}
